package com.yanqu.road.server.game.service.cmd;


import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractServerCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.game.service.IGameServiceCommand;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.sync.message.SyncResponse;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.S_GAME_SERVICE_HANDLER, desc = "游戏服服务")
public class GameServiceHandlerCmd extends AbstractServerCommand {

    private SyncMessage.Request request;

    @Override
    public boolean isDebug() {
        return true;
    }

    @Override
    public String getRequestData() {
        return "";
    }

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        this.request = SyncMessage.Request.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        IGameServiceCommand iGameServiceCommand = GamePlayerMgr.getSyncMessageProvider().getCommand(this.request.getCmdCode());
        if (iGameServiceCommand != null) {
            try {
                SyncMessage.Response.Builder builder = SyncMessage.Response.newBuilder();
                SyncResponse syncResponse = iGameServiceCommand.execute(this.request);
                builder.setErrorCode(syncResponse.getErrorCode());
                builder.setSequenceId(request.getSequenceId());
                builder.setForwardServerId(this.request.getRawServerId());
                long serverId = GameServer.getInstance().getServerId();
                builder.setActivityId(this.request.getActivityId());
                builder.setRawServerId(serverId);
                builder.setCmdCode(GameProtocol.S_CROSS_SERVICE_MESSAGE_RESP);
                if (syncResponse.getResponseBuilder() != null) {
                    builder.setDataBytes(syncResponse.getResponseBuilder().build().toByteString());
                }
                int code = CrossProtocol.C_SERVICE_BACK_MESSAGE;
                if (this.request.getActivityId() > 0) {
                    code = CrossProtocol.C_CROSS_ACTIVITY_RESP_HANDLER;
                } else if (this.request.getActivityId() == -1) {
                    code = CrossProtocol.C3_CROSS_RESP_HANDLER;
                }
                YanQuMessage yanQuMessage = YanQuMessageUtils.buildMessage(code, builder);
                GamePlayerMgr.sendPacket(this.request.getSenderId(), yanQuMessage);
            } catch (InvalidProtocolBufferException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void sendData(YanQuMessage packet) throws Exception {

    }


}
